package org.ailuoli.guard.api.controller;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ailuoli.guard.domain.entity.BusinessLog;
import org.ailuoli.guard.domain.service.BusinessLogService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.tools.bedrock.http.resp.RR;
import org.tools.bedrock.http.resp.v2.R;
import org.tools.bedrock.util.BaseUtils;

/**
 * @author sander
 * @date 2025/02/03
 **/
@Slf4j
@RestController
@RequestMapping("businessLog")
@RequiredArgsConstructor
public class BusinessLogController {

    private final BusinessLogService businessLogService;

    @GetMapping("listByBid")
    public RR list(@RequestParam("bid") String bid, @RequestParam(value = "creator", required = false, defaultValue = "") String isMyself) {
        Assert.notNull(bid, "bid is null");

        final LambdaQueryChainWrapper<BusinessLog> queryChainWrapper = businessLogService.lambdaQuery()
                .eq(BusinessLog::getBid, bid)
                .orderByDesc(BusinessLog::getCreatedAt);

        if (BaseUtils.isNotEmpty(isMyself)) {
            queryChainWrapper.eq(BusinessLog::getCreator, isMyself);
        }
        return R.ok(queryChainWrapper.list());
    }

}
